package oc.tm.sg.admin.web.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.smart4j.framework.tx.annotation.Service;

import oc.tm.sg.admin.govern.domain.SgRegistry;
import oc.tm.sg.admin.web.service.RegistriesService;
import oc.tm.sg.core.util.NetUtils;
import oc.tm.sg.registry.common.URL;
import oc.tm.sg.registry.core.Registry;
import oc.tm.sg.registry.core.support.AbstractRegistry;
import oc.tm.sg.registry.core.support.AbstractRegistryFactory;

/**
 * 注册中心控制器
 * @author lry
 */
@Service
public class RegistriesServiceImpl implements RegistriesService {

	/**
	 * 注册中心列表获取
	 * @param model
	 * @return
	 */
    public List<SgRegistry> getRegistries() {
        List<SgRegistry> rows = new ArrayList<SgRegistry>();
        //获取当前已使用过的注册中心
        Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
        if (registries.size() > 0) {
            SgRegistry sgRegistry;
            for (Registry registry : registries) {
                sgRegistry = new SgRegistry();
                sgRegistry.setServer(registry.getUrl().getAddress());
                sgRegistry.setHostname(NetUtils.getIpByAddress(sgRegistry.getServer()));
                sgRegistry.setAvailable(registry.isAvailable());
                if (registry instanceof AbstractRegistry) {
                    sgRegistry.setRegisteredCount(((AbstractRegistry) registry).getRegistered().size());
                    sgRegistry.setSubscribedCount(((AbstractRegistry) registry).getSubscribed().size());
                }
                rows.add(sgRegistry);
            }
        }
        return rows;
    }

    /**
     * 获取已注册列表
     * @param registry
     * @param model
     * @return
     */
    public List<String> registered(String registry) {
        Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
        Registry reg = null;
        if (registries.size() > 0) {
            for (Registry r : registries) {
                String sp = r.getUrl().getAddress();
                if (((registry == null || registry.length() == 0) && reg == null) || sp.equals(registry)) {
                    reg = r;
                }
            }
        }

        List<String> rows = new ArrayList<String>();
        if (reg instanceof AbstractRegistry) {
            Set<URL> services = ((AbstractRegistry) reg).getRegistered();
            if (services != null && services.size() > 0) {
                for (URL u : services) {
                    rows.add(u.toFullString());
                }
            }
        }

        return rows;
    }

    /**
     * 获取已订阅列表
     * @param registry
     * @param model
     * @return
     */
    public List<String> subscribed(String registry) {
        Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
        Registry reg = null;
        if (registries.size() > 0) {
            for (Registry r : registries) {
                String sp = r.getUrl().getAddress();
                if (((registry == null || registry.length() == 0) && reg == null) || sp.equals(registry)) {
                    reg = r;
                }
            }
        }
        List<String> rows = new ArrayList<String>();
        if (reg instanceof AbstractRegistry) {
            Set<URL> services = ((AbstractRegistry) reg).getSubscribed().keySet();
            if (services.size() > 0) {
                for (URL u : services) {
                    rows.add(u.toFullString());
                }
            }
        }
        return rows;
    }

}
